<!--  -->
<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <img src="@/assets/logo.png" alt="logo" class="logo" />
        <h2>749采购系统</h2>
      </div>
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="el-icon-user"
            placeholder="请输入用户名"
          />
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            prefix-icon="el-icon-lock"
            type="password"
            placeholder="请输入密码"
            @keyup.enter.native="handleLogin"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            :loading="loading"
            type="primary"
            class="login-button"
            @click="handleLogin"
          >
            {{ loading ? "登录中..." : "登录" }}
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        username: "",
        password: "",
      },
      loginRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 20,
            message: "长度在 3 到 20 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 3,
            max: 20,
            message: "长度在 3 到 20 个字符",
            trigger: "blur",
          },
        ],
      },
      loading: false,
    };
  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          // 模拟登录请求
          setTimeout(() => {
            // if (
            //   this.loginForm.username === "admin" &&
            //   this.loginForm.password === "123"
            // ) {
            //   localStorage.setItem("token", "your-token-value");
            //   this.$message.success("登录成功");
            //   this.$router.push("/home");
            // } else {
            //   this.$message.error("用户名或密码错误");
            // }
            // this.loading = false;
            // let flag = this.$store.dispatch("login", this.loginForm);
            // if (flag) {
            //   this.$message.success("登录成功");
            //   this.$router.push("/home");
            // } else {
            //   this.$message.error("用户名或密码错误");
            // }

            this.$store.dispatch("login", this.loginForm).then((success) => {
              if (success) {
                this.$router.push("/home"); // 跳转到主页面
              } else {
                alert("登录失败，请检查用户名和密码");
              }
            });

            this.loading = false;
          }, 1000);
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #1f4068 0%, #1a1b26 100%);
  overflow: hidden;
  position: relative;

  &::before {
    content: "";
    position: absolute;
    width: 2000px;
    height: 2000px;
    border-radius: 50%;
    background: linear-gradient(135deg, #43cea2 0%, #185a9d 100%);
    top: -10%;
    right: 48%;
    transform: translateY(-50%);
    z-index: 0;
    transition: 1.8s ease-in-out;
  }

  .login-box {
    width: 420px;
    padding: 40px;
    background: rgba(255, 255, 255, 0.9);
    border-radius: 10px;
    box-shadow: 0 15px 25px rgba(0, 0, 0, 0.2);
    position: relative;
    z-index: 1;
    backdrop-filter: blur(10px);

    .login-header {
      text-align: center;
      margin-bottom: 40px;

      .logo {
        width: 84px;
        margin-bottom: 20px;
      }

      h2 {
        margin: 0;
        font-size: 24px;
        color: #303133;
        font-weight: 600;
      }
    }

    .login-form {
      .el-input {
        height: 40px;

        /deep/ .el-input__inner {
          height: 40px;
          padding-left: 45px;
          background: rgba(255, 255, 255, 0.8);
          border: 1px solid #dcdfe6;
          color: #606266;

          &:focus {
            border-color: #409eff;
            background: #fff;
          }
        }

        /deep/ .el-input__prefix {
          left: 15px;
          color: #909399;
        }
      }

      .login-button {
        width: 100%;
        height: 40px;
        font-size: 16px;
        font-weight: 500;
        background: linear-gradient(135deg, #43cea2 0%, #185a9d 100%);
        border: none;
        padding: 0;
        margin-top: 10px;

        &:hover,
        &:focus {
          background: linear-gradient(135deg, #3db392 0%, #154e89 100%);
        }
      }
    }
  }
}

// 响应式设计
@media screen and (max-width: 768px) {
  .login-container {
    .login-box {
      width: 90%;
      max-width: 420px;
      padding: 30px 20px;
    }
  }
}
</style>
